//
//  HXIflyAllGroupPeopleViewController.m
//  KD
//
//  Created by GaoFei on 16/7/8.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXIflyAllGroupPeopleViewController.h"
//#import "HXAllGroupPeopleCollectionView.h"

#import "HXIflyPersonalDataTableViewController.h"//个人资料
#import "HXGetGroupMembersModel.h"

@interface HXIflyAllGroupPeopleViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,copy) NSString * leftBarTitle;
@property (nonatomic,copy) NSString * groupsId;
//@property (nonatomic,strong) HXAllGroupPeopleCollectionView * collectionView;

/**
 群组列表
 */
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray <HXGetGroupMembersListModel *> * dataArr;

@end

@implementation HXIflyAllGroupPeopleViewController

//创建群组成员
-(instancetype)initWithTitleName:(NSString *)tileName withGroupsId:(NSString *)groupsId{

    self = [super init];
    
    if (self) {
        
        self.leftBarTitle = tileName;
        self.groupsId = groupsId;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.leftBarTitle;
    
//    //创建导航栏的左侧的btn按钮
//    UIButton * leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 120, 40)];
//    leftBtn.contentMode = UIViewContentModeLeft;
//    leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    // 让按钮的内容往左边偏移10
//    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
//    [leftBtn setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
//    [leftBtn setTitle:self.leftBarTitle forState:UIControlStateNormal];
//    [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [leftBtn addTarget:self action:@selector(leftBarBtnAct:) forControlEvents:UIControlEventTouchUpInside];
//    [leftBtn sizeToFit];//让按钮大小跟随内容
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
//    //创建UICollectionView
//    if (_collectionView == nil) {
//        
//        _collectionView = [[HXAllGroupPeopleCollectionView alloc] initWithFrame:CGRectMake(0, 0, HXSCREEN_WIDTH, HXSCREEN_HEIGHT)];
//        
//        [self.view addSubview:_collectionView];
//        
//        _collectionView.dataArr = @[@"1",
//                                    @"1",
//                                    @"1",
//                                    @"1",
//                                    @"1",
//                                    @"1",
//                                    @"1",
//                                    @"1",
//                                    @"1",
//                                    @"1",
//                                    @"1",
//                                    @"1",];
//    }
    
    //创建UITableView
    [self loadCreateUITableView];
    
    //加载数据
    [self loadCreateData];
}

/**
 加载数据
 */
-(void)loadCreateData{
    [SVProgressHUD showWithStatus:@"正在加载..."];
    [NetworkAPI getGroupsMembersWithGroupId:self.groupsId withMembersId:@"" Success:^(HXGetGroupMembersModel *result) {
        if ([result.code isEqualToString:@"200"]) {
            
            [SVProgressHUD dismiss];
            self.dataArr = [result.result mutableCopy];
            
            [self.tableView reloadData];
        }
    } failure:^(NSError *err) {
        [SVProgressHUD showErrorWithStatus:@"网络错误!"];
    }];
}

/**
 创建UITableView
 */
-(void)loadCreateUITableView{

    if (_tableView == nil) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, HXSCREEN_WIDTH, HXSCREEN_HEIGHT) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
}

#pragma mark -----// UITableViewDataSource \\------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"identifierCell"];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifierCell"];
        
//        cell.backgroundColor = [UIColor yellowColor];
    }
    
    HXGetGroupMembersListModel * model = self.dataArr[indexPath.row];
    
    cell.textLabel.text = model.PersonName;
    cell.imageView.image = [UIImage imageNamed:@"tx"];
    
    return cell;
}

//单元格的大小
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 48.f;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return [NSString stringWithFormat:@"全部组成员(%ld)",self.dataArr.count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 36.f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    HXGetGroupMembersListModel * model = self.dataArr[indexPath.row];
    
//    HXIflyPersonalDataTableViewController * personalDataVc = [[HXIflyPersonalDataTableViewController alloc] initWithGroupMembersModel:model withGroupsId:self.groupsId];
    
    HXIflyPersonalDataTableViewController * personalDataVc = [[HXIflyPersonalDataTableViewController alloc] initWithUserID:model.MemberID withGroupsId:self.groupsId];

    
    
    [self.navigationController pushViewController:personalDataVc animated:YES];
}

/**
 左侧导航栏的点击相应事件
 */
//-(void)leftBarBtnAct:(UIButton *)sender{
//
//    [self.navigationController popViewControllerAnimated:YES];
//}


#pragma mark -----// 懒加载 \\----------
//-(UITableView *)tableView{
//
//    if (_tableView == nil) {
//        
//        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, HXSCREEN_WIDTH, HXSCREEN_HEIGHT)];
//        _tableView.delegate = self;
//        _tableView.dataSource = self;
//        [self.view addSubview:_tableView];
//    }
//    
//    return _tableView;
//}

-(NSMutableArray<HXGetGroupMembersListModel *> *)dataArr{
    
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
